package org.smartercontext.contextmger.converters;

import java.util.ArrayList;
import java.util.List;

import org.smartercontext.contextmger.DTO.BooksDTO;
import org.smartercontext.contextmger.DTO.MovieDTO;
import org.smartercontext.contextmger.DTO.MusicDTO;
import org.smartercontext.contextmger.DTO.PersonDTO;
import org.smartercontext.contextmger.DTO.StudyDTO;
import org.smartercontext.contextmger.DTO.WorkDTO;
import org.smartercontext.contextmger.contextsphere.ContextManagerController;
import org.smartercontext.contextmger.locationgatherer.CompleteGeolocation;

import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;

public class RDFConverter {
	

	public RDFConverter(){
		
	}
	
	public Model writeLocation(Model usermodel, CompleteGeolocation Usergeolocation){
		
		ContextManagerController.contextManagerController();
		OntModel modelPlaces = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
		modelPlaces.setNsPrefixes(ContextManagerController.getMappings());
		Property geoPropr = modelPlaces.createProperty(ContextManagerController.getNsSchemaGC(),"GeoLocation");
		Property rdfType = modelPlaces.createProperty(ContextManagerController.getRdf(),"type");
		Property locatedIn = modelPlaces.createDatatypeProperty(
				ContextManagerController.getNsSchemaGC()
				+ "locatedIn");
		
		DatatypeProperty geoLocationClassification = 
				modelPlaces.createDatatypeProperty(ContextManagerController.getNsSchemaGC()
						+ "geoLocationClassification");
		DatatypeProperty propLatitude = modelPlaces.createDatatypeProperty(ContextManagerController.getNsSchemaGC()+"latitude");
		DatatypeProperty propLongitude = modelPlaces.createDatatypeProperty(ContextManagerController.getNsSchemaGC()+"longitude");
		DatatypeProperty propZipcode = modelPlaces.createDatatypeProperty(ContextManagerController.getNsSchemaGC()+"zipcode");
		DatatypeProperty propaddress = modelPlaces.createDatatypeProperty(ContextManagerController.getNsSchemaGC()+"address");
		
		Resource country = null;
		if(Usergeolocation.getCountry()!=null){
			
			country = modelPlaces.createResource(ContextManagerController.getNsCitiesModel()+Usergeolocation.getCountry());
			usermodel.add(country,rdfType,geoPropr);
			Literal countryL = modelPlaces.createTypedLiteral(new String("Country"));
			usermodel.addLiteral(country, geoLocationClassification, countryL);
			
			if(Usergeolocation.getCity()==null&&Usergeolocation.getNeighborhood()==null && Usergeolocation.getPlace()==null){
				
				Literal latitude = modelPlaces.createTypedLiteral(new String(Usergeolocation.getLatitude())); 
				Literal longitude = modelPlaces.createTypedLiteral(new String(Usergeolocation.getLongitude())); 
				
				usermodel.addLiteral(country, propLatitude, latitude);
				usermodel.addLiteral(country, propLongitude, longitude);
				
				if(Usergeolocation.getZipCode()!=null){
					Literal zipcode = modelPlaces.createTypedLiteral(new String(Usergeolocation.getZipCode()));
					usermodel.addLiteral(country, propZipcode, zipcode);
				}
				
				Literal address = modelPlaces.createTypedLiteral(new String(Usergeolocation.getComplete_address()));
				usermodel.addLiteral(country, propaddress, address);
				
			}
			
		}
		
		Resource city = null;
		
		if(Usergeolocation.getCity()!=null){
			
			if(country!=null){
				
				city = modelPlaces.createResource(ContextManagerController.getNsCitiesModel()+Usergeolocation.getCity());
				usermodel.add(city, rdfType, geoPropr);
				Literal cityL = modelPlaces.createTypedLiteral(new String("City"));
				usermodel.addLiteral(city, geoLocationClassification, cityL);
				usermodel.add(city,locatedIn,country);
			}else{
				city = modelPlaces.createResource(ContextManagerController.getNsCitiesModel()+Usergeolocation.getCity());
				usermodel.add(city, rdfType, geoPropr);
				Literal cityL = modelPlaces.createTypedLiteral(new String("City"));
				usermodel.addLiteral(city, geoLocationClassification, cityL);
			}
			
			if(	Usergeolocation.getNeighborhood()==null && Usergeolocation.getPlace()==null){
				
				Literal latitude = modelPlaces.createTypedLiteral(new String(Usergeolocation.getLatitude())); 
				Literal longitude = modelPlaces.createTypedLiteral(new String(Usergeolocation.getLongitude())); 
				
				usermodel.addLiteral(city, propLatitude, latitude);
				usermodel.addLiteral(city, propLongitude, longitude);
				
				if(Usergeolocation.getZipCode()!=null){
					Literal zipcode = modelPlaces.createTypedLiteral(new String(Usergeolocation.getZipCode()));
					usermodel.addLiteral(city, propZipcode, zipcode);
				}
				
				Literal address = modelPlaces.createTypedLiteral(new String(Usergeolocation.getComplete_address()));
				usermodel.addLiteral(city, propaddress, address);
				
			}
		}
		
		Resource neighb = null;
		
		if(Usergeolocation.getNeighborhood()!=null){
			
			if(city!=null){
				neighb = modelPlaces.createResource(ContextManagerController.getNsCitiesModel()+Usergeolocation.getNeighborhood());
				usermodel.add(neighb,rdfType,geoPropr);
				Literal neigbL = modelPlaces.createTypedLiteral(new String("Neighborhood"));
				usermodel.addLiteral(neighb, geoLocationClassification, neigbL);
				usermodel.add(neighb,locatedIn,city);
				
			}else if(country!=null){
				neighb = modelPlaces.createResource(ContextManagerController.getNsCitiesModel()+Usergeolocation.getNeighborhood());
				usermodel.add(neighb,rdfType,geoPropr);
				Literal neigbL = modelPlaces.createTypedLiteral(new String("Neighborhood"));
				usermodel.addLiteral(neighb, geoLocationClassification, neigbL);
				usermodel.add(neighb,locatedIn,country);
			}else{
				neighb = modelPlaces.createResource(ContextManagerController.getNsCitiesModel()+Usergeolocation.getNeighborhood());
				usermodel.add(neighb,rdfType,geoPropr);
				Literal neigbL = modelPlaces.createTypedLiteral(new String("Neighborhood"));
				usermodel.addLiteral(neighb, geoLocationClassification, neigbL);
			}
			
			if(Usergeolocation.getPlace()==null){
				
				Literal latitude = modelPlaces.createTypedLiteral(new String(Usergeolocation.getLatitude())); 
				Literal longitude = modelPlaces.createTypedLiteral(new String(Usergeolocation.getLongitude())); 
				
				usermodel.addLiteral(neighb, propLatitude, latitude);
				usermodel.addLiteral(neighb, propLongitude, longitude);
				
				if(Usergeolocation.getZipCode()!=null){
					Literal zipcode = modelPlaces.createTypedLiteral(new String(Usergeolocation.getZipCode()));
					usermodel.addLiteral(neighb, propZipcode, zipcode);
				}
				
				Literal address = modelPlaces.createTypedLiteral(new String(Usergeolocation.getComplete_address()));
				usermodel.addLiteral(neighb, propaddress, address);
				
			}
		}
		
		Resource place = null;
		
		if(Usergeolocation.getPlace()!=null){
			
			if(neighb!=null){
				place = modelPlaces.createResource(ContextManagerController.getNsCitiesModel()+Usergeolocation.getPlace());
				usermodel.add(place,rdfType,geoPropr);
				Literal placeL = modelPlaces.createTypedLiteral(new String("Place"));
				usermodel.addLiteral(place, geoLocationClassification, placeL);
				usermodel.add(place,locatedIn,neighb);
			}else if(city!=null){
				place = modelPlaces.createResource(ContextManagerController.getNsCitiesModel()+Usergeolocation.getPlace());
				usermodel.add(place,rdfType,geoPropr);
				Literal placeL = modelPlaces.createTypedLiteral(new String("Place"));
				usermodel.addLiteral(place, geoLocationClassification, placeL);
				usermodel.add(place,locatedIn,city);
			}else if(country!=null){
				place = modelPlaces.createResource(ContextManagerController.getNsCitiesModel()+Usergeolocation.getPlace());
				usermodel.add(place,rdfType,geoPropr);
				Literal placeL = modelPlaces.createTypedLiteral(new String("Place"));
				usermodel.addLiteral(place, geoLocationClassification, placeL);
				usermodel.add(place,locatedIn,country);
			}else{
				place = modelPlaces.createResource(ContextManagerController.getNsCitiesModel()+Usergeolocation.getPlace());
				usermodel.add(place,rdfType,geoPropr);
				Literal placeL = modelPlaces.createTypedLiteral(new String("Place"));
				usermodel.addLiteral(place, geoLocationClassification, placeL);
			}
			

				Literal latitude = modelPlaces.createTypedLiteral(new String(Usergeolocation.getLatitude())); 
				Literal longitude = modelPlaces.createTypedLiteral(new String(Usergeolocation.getLongitude())); 
				
				usermodel.addLiteral(place, propLatitude, latitude);
				usermodel.addLiteral(place, propLongitude, longitude);
				
				if(Usergeolocation.getZipCode()!=null){
					Literal zipcode = modelPlaces.createTypedLiteral(new String(Usergeolocation.getZipCode()));
					usermodel.addLiteral(place, propZipcode, zipcode);
				}
				
				Literal address = modelPlaces.createTypedLiteral(new String(Usergeolocation.getComplete_address()));
				usermodel.addLiteral(place, propaddress, address);
				
		}
		
		String nsUser="http://smartercontext.org/vocabularies/rdf/"+Usergeolocation.getUser()+".rdf#";
		
		Resource resource = usermodel.getResource(nsUser+Usergeolocation.getUser());
		resource.removeAll(locatedIn);
		
		System.out.println("*");
		if(place!=null){
			System.out.println("**");
			usermodel.add(resource,locatedIn,place);
			
		}else if(neighb!=null){
			System.out.println("*****");
			usermodel.add(resource,locatedIn,neighb);
			
		}else if(city!=null){
			System.out.println("*******");
			usermodel.add(resource,locatedIn,city);

			System.out.println("**********");
			usermodel.add(resource,locatedIn,country);
		}
		
		return usermodel;
	}
	
	//Cambiar el retorno por Model
	public Model writeFBContextInf(ArrayList<ArrayList<Object>>preferences, Model user,
			String facebookId, String genre, String birthday, String relationshipStatus, String significantOther,
			String username)
					throws Exception{
		
		ContextManagerController.contextManagerController();
		Model userModel = user;
		
		if(preferences.get(0)!=null){
			
			OntModel modelStudies = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
			modelStudies.setNsPrefixes(ContextManagerController.getMappings());
						
			ArrayList<Object>studies = preferences.get(0);
			
			//List<Statement>statementStudies = new ArrayList<>();
			String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
			
			for(int i=0;i<studies.size();i++){
				
				StudyDTO temp = (StudyDTO)studies.get(i);
				String name = temp.getSchoolName();
				
				Resource placeResource = modelStudies.createResource(nsUser+name);
				Property rdfType = modelStudies.createProperty(ContextManagerController.getRdf(),"type");
				Property placeProp = modelStudies.createProperty(ContextManagerController.getNsSchemaGC(),"GroupEntity");
				
				userModel.add(placeResource,rdfType,placeProp);
				
				Literal clasification = modelStudies.createTypedLiteral(new String(temp.getSchoolType())); 
				
				DatatypeProperty studiesClasification = modelStudies.createDatatypeProperty(
						ContextManagerController.getNsSchemaPERSONAL()
						+ "studiesClassification");
				
								
				userModel.addLiteral(placeResource, studiesClasification, clasification);
				
				Resource resource = userModel.getResource(nsUser+username);
				
				if(temp.getEndYear()!=null || temp.getEndYear()!=""){
					//is studiedAt
					Property studiedAt = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"studiedAt");
					userModel.add(resource, studiedAt, placeResource);
				}else{
					//is studiesAt
					Property studiesAt = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"studiesAt");
					userModel.add(resource, studiesAt, placeResource);
					
				}
			}
			
		}
		
		if(preferences.get(1)!=null){
			
			OntModel modelFamily = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
			modelFamily.setNsPrefixes(ContextManagerController.getMappings());
			
			ArrayList<Object>family = preferences.get(1);
			
			String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
			
			for(int i=0;i<family.size();i++){
				
				PersonDTO pdto = (PersonDTO)family.get(i);
				String name = pdto.getName();
				String fbId = pdto.getFacebookId();
				String relType = pdto.getRelationship();
				
				Resource personResource = modelFamily.createResource(nsUser+name);
				Property rdfType = modelFamily.createProperty(ContextManagerController.getRdf(),"type");
				Property personProp = modelFamily.createProperty(ContextManagerController.getNsSchemaGC(),"HumanEntity");
				
				userModel.add(personResource,rdfType,personProp);
				
				Literal facebookIdL = modelFamily.createTypedLiteral(new String(fbId));
				
				DatatypeProperty dtpfbId = modelFamily.createDatatypeProperty(
						ContextManagerController.getNsSchemaPERSONAL()
						+ "facebookId");
				
				userModel.addLiteral(personResource, dtpfbId, facebookIdL);
				
				Resource resource = userModel.getResource(nsUser+username);
				
				if(relType!=null){
					if(relType.equals("sister")){
						
						Property sister = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsSister");
						userModel.add(resource, sister, personResource);
						
					}else if(relType.equals("brother")){
						
						Property brother = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsBrother");
						userModel.add(resource, brother, personResource);
						
					}else if(relType.equals("mother")){
						
						Property mother = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"childOf");
						userModel.add(resource, mother, personResource);
						
					}else if(relType.equals("father")){
						
						Property father = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"childOf");
						userModel.add(resource, father, personResource);
						
					}else if(relType.equals("daugther")){
						
						Property daughter = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"parentOf");
						userModel.add(resource, daughter, personResource);
						
					}else if(relType.equals("son")){
						
						Property son = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"parentOf");
						userModel.add(resource, son, personResource);
						
					}else if(relType.equals("aunt")){
						
						Property aunt = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsAunt");
						userModel.add(resource, aunt, personResource);
						
					}else if(relType.equals("uncle")){
						
						Property uncle = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsUncle");
						userModel.add(resource, uncle, personResource);
						
					}else if(relType.equals("niece")){
						
						Property niece = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsNiece");
						userModel.add(resource, niece, personResource);
						
					}else if(relType.equals("nephew")){
						
						Property nephew = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsNephew");
						userModel.add(resource, nephew, personResource);
						
					}else if(relType.equals("cousin")){
						
						Property cousin = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsCousin");
						userModel.add(resource, cousin, personResource);
						
					}else if(relType.equals("grandmother")){
						
						Property grandma = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsGrandMa");
						userModel.add(resource, grandma, personResource);
						
					}else if(relType.equals("grandfather")){
						
						Property grandpa = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsGrandPa");
						userModel.add(resource, grandpa, personResource);
						
					}else if(relType.equals("granddaugther")){
						
						Property gchild = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsGrandChild");
						userModel.add(resource, gchild, personResource);
						
					}else if(relType.equals("grandson")){
						
						Property gchild = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsGrandChild");
						userModel.add(resource, gchild, personResource);
						
					}else if(relType.equals("stepsister")){
						
						Property stepSis = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsStepSister");
						userModel.add(resource, stepSis, personResource);
						
					}else if(relType.equals("stepbrother")){
						
						Property stepBro = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsStepBrother");
						userModel.add(resource, stepBro, personResource);
						
					}else if(relType.equals("stepmother")){
						
						Property stepmother = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsStepMother");
						userModel.add(resource, stepmother, personResource);
						
					}else if(relType.equals("stepfather")){
						
						Property stepfather = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsStepFather");
						userModel.add(resource, stepfather, personResource);
						
					}else if(relType.equals("stepdaugther")){
						
						Property stepdaugther = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsStepDaugther");
						userModel.add(resource, stepdaugther, personResource);
						
					}else if(relType.equals("stepson")){
						
						Property stepson = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsStepSon");
						userModel.add(resource, stepson, personResource);
						
					}else if(relType.equals("sister-in-law")){
						
						Property sislaw = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsSisterInLaw");
						userModel.add(resource, sislaw, personResource);
						
					}else if(relType.equals("brother-in-law")){
						
						Property brolaw = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsBroInLaw");
						userModel.add(resource, brolaw, personResource);
						
					}else if(relType.equals("mother-in-law")){
						
						Property mothlaw = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsMotherInLaw");
						userModel.add(resource, mothlaw, personResource);
						
					}else if(relType.equals("father-in-law")){
						
						Property fathlaw = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsFatherInLaw");
						userModel.add(resource, fathlaw, personResource);
						
					}else if(relType.equals("daugther-in-law")){
						
						Property daughlaw = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsDaugtherInLaw");
						userModel.add(resource, daughlaw, personResource);
						
					}else if(relType.equals("son-in-law")){
						
						Property sonlaw = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsSonInLaw");
						userModel.add(resource, sonlaw, personResource);
					}else {
						
						Property famGen = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasAsFamilyMember");
						userModel.add(resource, famGen, personResource);
					}
				}
				
			}
		}
		
		if(preferences.get(2)!=null){
			
			OntModel modelFriends = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
			modelFriends.setNsPrefixes(ContextManagerController.getMappings());
			
			ArrayList<Object>friends = preferences.get(2);
			
			String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
			
			for(int i=0;i<friends.size();i++){
				PersonDTO pdto = (PersonDTO)friends.get(i);
				String name = pdto.getName();
				String fbId = pdto.getFacebookId();
				
				Resource personResource = modelFriends.createResource(nsUser+name);
				Property rdfType = modelFriends.createProperty(ContextManagerController.getRdf(),"type");
				Property personProp = modelFriends.createProperty(ContextManagerController.getNsSchemaGC(),"HumanEntity");
				
				userModel.add(personResource,rdfType,personProp);
				
				Literal facebookIdL = modelFriends.createTypedLiteral(new String(fbId));
				
				DatatypeProperty dtpfbId = modelFriends.createDatatypeProperty(
						ContextManagerController.getNsSchemaPERSONAL()
						+ "facebookId");
				
				userModel.addLiteral(personResource, dtpfbId, facebookIdL);
				
				Resource resource = userModel.getResource(nsUser+username);
				
				Property friend = modelFriends.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"friendOf");
				userModel.add(resource, friend, personResource);
			}
		}
		
		if(preferences.get(3)!=null){
			
			OntModel modelMovies = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
			modelMovies.setNsPrefixes(ContextManagerController.getMappings());
			
			ArrayList<Object>movies = preferences.get(3);
			
			String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
			
			for(int i=0;i<movies.size();i++){
				MovieDTO mdto = (MovieDTO)movies.get(i);
				String name = mdto.getName();
				
				Resource resource = modelMovies.createResource(nsUser+""+name);
				Property rdfType = modelMovies.createProperty(ContextManagerController.getRdf(),"type");
				Property movieType = modelMovies.createProperty(ContextManagerController.getNsSchemaGC()+"ArtificialEntity");
				
				userModel.add(resource, rdfType, movieType);
				
				DatatypeProperty artifCat = modelMovies.createDatatypeProperty(ContextManagerController.getNsSchemaGC()+"artificialCategory");
				Literal artifCatL = modelMovies.createTypedLiteral(new String("Movies"));
				userModel.addLiteral(resource, artifCat, artifCatL);
				
				Resource resource2 = userModel.getResource(nsUser+username);
				Property likes = modelMovies.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"likes");
				userModel.add(resource2, likes, resource);
				
			}
			
		}
		
		if(preferences.get(4)!=null){
			
			OntModel modelMusic = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
			modelMusic.setNsPrefixes(ContextManagerController.getMappings());
			
			ArrayList<Object>music = preferences.get(4);
			
			String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
			
			for(int i=0;i<music.size();i++){
				
				MusicDTO mdto = (MusicDTO) music.get(i);
				String name = mdto.getName();
				Resource resource = modelMusic.createResource(nsUser+""+name);
				Property rdfType = modelMusic.createProperty(ContextManagerController.getRdf(),"type");
				Property musicType = modelMusic.createProperty(ContextManagerController.getNsSchemaGC()+"ArtificialEntity");
				
				userModel.add(resource, rdfType, musicType);
				
				DatatypeProperty artifCat = modelMusic.createDatatypeProperty(ContextManagerController.getNsSchemaGC()+"artificialCategory");
				Literal artifCatL = modelMusic.createTypedLiteral(new String("Music"));
				userModel.addLiteral(resource, artifCat, artifCatL);
				
				Resource resource2 = userModel.getResource(nsUser+username);
				Property likes = modelMusic.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"likes");
				userModel.add(resource2, likes, resource);
				
				
			}
			
			
		}
		
		if(preferences.get(5)!=null){
			
			OntModel modelBooks = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
			modelBooks.setNsPrefixes(ContextManagerController.getMappings());
			
			ArrayList<Object>books = preferences.get(5);
			
			String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
			
			for(int i=0;i<books.size();i++){
				BooksDTO bdto = (BooksDTO)books.get(i);
				String name = bdto.getName();
				
				Resource resource = modelBooks.createResource(nsUser+""+name);
				Property rdfType = modelBooks.createProperty(ContextManagerController.getRdf(),"type");
				Property bookType = modelBooks.createProperty(ContextManagerController.getNsSchemaGC()+"ArtificialEntity");
				
				userModel.add(resource, rdfType, bookType);
				
				DatatypeProperty artifCat = modelBooks.createDatatypeProperty(ContextManagerController.getNsSchemaGC()+"artificialCategory");
				Literal artifCatL = modelBooks.createTypedLiteral(new String("Books"));
				userModel.addLiteral(resource, artifCat, artifCatL);
				
				Resource resource2 = userModel.getResource(nsUser+username);
				Property likes = modelBooks.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"likes");
				userModel.add(resource2, likes, resource);
			}
			
		}
		
		if(preferences.get(6)!=null){
			
			OntModel modelWork = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
			modelWork.setNsPrefixes(ContextManagerController.getMappings());
			DatatypeProperty geoLocationClassification = 
					modelWork.createDatatypeProperty(ContextManagerController.getNsSchemaGC()
							+ "geoLocationClassification");
			
			ArrayList<Object>work = preferences.get(6);
			
			String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
			
			for(int i=0;i<work.size();i++){
				WorkDTO wdto = (WorkDTO) work.get(i);
				String name = wdto.getEmployerName();
				String location = wdto.getEmployerLoc();
				
				if(location!=null){
				
				Resource workResource = modelWork.createResource(nsUser+name);
				Property rdfType = modelWork.createProperty(ContextManagerController.getRdf(),"type");
				Property prdOrService = modelWork.createProperty(ContextManagerController.getNsSchemaGC(),"GroupEntity");
				
				userModel.add(workResource,rdfType,prdOrService);
				
				Resource locale = modelWork.createResource(ContextManagerController.getNsCitiesModel()+location);
				Property geoPropr = modelWork.createProperty(ContextManagerController.getNsSchemaGC(),"GeoLocation");
				userModel.add(locale,rdfType,geoPropr);
				Literal locClass = modelWork.createTypedLiteral(new String("City"));
				userModel.addLiteral(locale, geoLocationClassification, locClass);
				
				Property locatedIn = modelWork.createDatatypeProperty(
						ContextManagerController.getNsSchemaGC()
						+ "locatedIn");
				
				if(location!=""&&location!=null){
				userModel.add(workResource, locatedIn, locale);
				}
								
				Resource resource = userModel.getResource(nsUser+username);
				
				if(wdto.getEndDate()!=null&&wdto.getEndDate()!=""){
					//is workedAt
					Property workedAt = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"workedAt");
					userModel.add(resource, workedAt, workResource);
				}else{
					//is workAt
					Property worksAt = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"worksAt");
					userModel.add(resource, worksAt, workResource);
				}
				}
			}
		}
		
		String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
		Resource baseResource = userModel.getResource(nsUser+username);
		
		Literal gender = userModel.createLiteral("NOT SPECIFIED");
		
		if(genre.equals("female")){
			gender = userModel.createLiteral("FEMALE");
		}else if(genre.equals("male")){
			gender = userModel.createLiteral("MALE");
		}
		
		Property genderP = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"hasGender");
		userModel.add(baseResource, genderP, gender);
		
		if(birthday!=null&&birthday!=""){
			
			Literal birthDayL = userModel.createTypedLiteral(new String(birthday));
			Property birthdayP = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"birthYear");
			userModel.add(baseResource, birthdayP, birthDayL);
			
		}
		
		if(relationshipStatus!=null&&relationshipStatus!=""){
			
			Literal relstatusL = userModel.createLiteral(relationshipStatus);
			Property relstatusP = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"relationShipStatus");
			userModel.add(baseResource, relstatusP, relstatusL);
			
		}
		
		if(significantOther!=null&&significantOther!=""){
			
			Resource res = userModel.getResource(nsUser+significantOther);
			Property engagedTo = userModel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"engagedTo");
			res.removeAll(engagedTo);
			userModel.add(baseResource, engagedTo, res);
		}
		
				
		return userModel;
	}
	
	public Model writeBuyerPreferences(List<String>wishes, List<String>productSeen, List<String>productsRanked, Model usermodel
			, String username){
		
		Model userModel = usermodel;
		
		String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
		String nsProducts="http://smartercontext.org/vocabularies/rdf/products.rdf#";
		
		ContextManagerController.contextManagerController();
		
		if(wishes.size()>0){
			
			OntModel modelProducts = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
			modelProducts.setNsPrefixes(ContextManagerController.getMappings());
			
			for(int i=0;i<wishes.size();i++){
				String[] wish = wishes.get(i).split("#");
				String name = wish[0];
				String eanucc = wish[1];
				String gtin8 = wish[2];
				String gtin14 = wish[3];
				
				Resource productResource = modelProducts.createResource(nsProducts+name);
				
				Property rdfType = modelProducts.createProperty(ContextManagerController.getRdf(),"type");
				Property prdOrService = modelProducts.createProperty(ContextManagerController.getNsSchemaGR(),"ProductOrService");
				
				userModel.add(productResource, rdfType, prdOrService);
				
				Literal eanuccL = modelProducts.createTypedLiteral(new String(eanucc));
				DatatypeProperty hasEANUCC = modelProducts.createDatatypeProperty(ContextManagerController.
						getNsSchemaGR()+"hasEAN_UCC-13");
				Literal gtin8L = modelProducts.createTypedLiteral(new String(gtin8));
				DatatypeProperty hasGTIN8 = modelProducts.createDatatypeProperty(ContextManagerController.
						getNsSchemaGR()+"hasGTIN-8");
				Literal gtin14L = modelProducts.createTypedLiteral(new String(gtin14));
				DatatypeProperty hasGTIN14 = modelProducts.createDatatypeProperty(ContextManagerController.
						getNsSchemaGR()+"hasGTIN-14");
				
				userModel.addLiteral(productResource, hasEANUCC, eanuccL);
				userModel.addLiteral(productResource, hasGTIN8, gtin8L);
				userModel.addLiteral(productResource, hasGTIN14, gtin14L);
				
				Resource resource = userModel.getResource(nsUser+username);
				Property wishesP = modelProducts.createProperty(ContextManagerController.getNsSchemaShopping(),"wishes");
				userModel.add(resource,wishesP,productResource);
				
				
			}
			
		}
			
		if(productSeen.size()>0){
			
			OntModel modelProducts = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
			modelProducts.setNsPrefixes(ContextManagerController.getMappings());
			
			for(int i=0;i<productSeen.size();i++){
				String []pSeen = productSeen.get(i).split("#");
				String name = pSeen[0];
				String eanucc = pSeen[1];
				String gtin8 = pSeen[2];
				String gtin14 = pSeen[3];
				
				Resource productResource = modelProducts.createResource(nsProducts+name);
				
				Property rdfType = modelProducts.createProperty(ContextManagerController.getRdf(),"type");
				Property prdOrService = modelProducts.createProperty(ContextManagerController.getNsSchemaGR(),"ProductOrService");
				
				userModel.add(productResource, rdfType, prdOrService);
				
				Literal eanuccL = modelProducts.createTypedLiteral(new String(eanucc));
				DatatypeProperty hasEANUCC = modelProducts.createDatatypeProperty(ContextManagerController.
						getNsSchemaGR()+"hasEAN_UCC-13");
				Literal gtin8L = modelProducts.createTypedLiteral(new String(gtin8));
				DatatypeProperty hasGTIN8 = modelProducts.createDatatypeProperty(ContextManagerController.
						getNsSchemaGR()+"hasGTIN-8");
				Literal gtin14L = modelProducts.createTypedLiteral(new String(gtin14));
				DatatypeProperty hasGTIN14 = modelProducts.createDatatypeProperty(ContextManagerController.
						getNsSchemaGR()+"hasGTIN-14");
				
				userModel.addLiteral(productResource, hasEANUCC, eanuccL);
				userModel.addLiteral(productResource, hasGTIN8, gtin8L);
				userModel.addLiteral(productResource, hasGTIN14, gtin14L);
				
				Resource resource = userModel.getResource(nsUser+username);
				Property pseen = modelProducts.createProperty(ContextManagerController.getNsSchemaShopping(),"productSeen");
				userModel.add(resource,pseen,productResource);
			}
			
			
		}
		
		if(productsRanked.size()>0){
			
			OntModel modelProducts = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
			modelProducts.setNsPrefixes(ContextManagerController.getMappings());
			
			for(int i=0;i<productsRanked.size();i++){
				String[] pRank = productsRanked.get(i).split("#");
				String value = pRank[0];
				String name = pRank[1];
				String eanucc = pRank[2];
				String gtin8 = pRank[3];
				String gtin14 = pRank[4];
				
				Resource productResource = modelProducts.createResource(nsProducts+name);
				
				Property rdfType = modelProducts.createProperty(ContextManagerController.getRdf(),"type");
				Property prdOrService = modelProducts.createProperty(ContextManagerController.getNsSchemaGR(),"ProductOrService");
				
				userModel.add(productResource, rdfType, prdOrService);
				
				Literal rvalueL = modelProducts.createTypedLiteral(Integer.parseInt(value));
				DatatypeProperty rankingValue = modelProducts.createDatatypeProperty(ContextManagerController.
						getNsSchemaPERSONAL()+"rankingValue");
				Literal eanuccL = modelProducts.createTypedLiteral(new String(eanucc));
				DatatypeProperty hasEANUCC = modelProducts.createDatatypeProperty(ContextManagerController.
						getNsSchemaGR()+"hasEAN_UCC-13");
				Literal gtin8L = modelProducts.createTypedLiteral(new String(gtin8));
				DatatypeProperty hasGTIN8 = modelProducts.createDatatypeProperty(ContextManagerController.
						getNsSchemaGR()+"hasGTIN-8");
				Literal gtin14L = modelProducts.createTypedLiteral(new String(gtin14));
				DatatypeProperty hasGTIN14 = modelProducts.createDatatypeProperty(ContextManagerController.
						getNsSchemaGR()+"hasGTIN-14");
				
				userModel.addLiteral(productResource, hasEANUCC, eanuccL);
				userModel.addLiteral(productResource, hasGTIN8, gtin8L);
				userModel.addLiteral(productResource, hasGTIN14, gtin14L);
				userModel.addLiteral(productResource, rankingValue, rvalueL);
				
				Resource resource = userModel.getResource(nsUser+username);
				Property ranked = modelProducts.createProperty(ContextManagerController.getNsSchemaShopping(),"ranked");
				userModel.add(resource,ranked,productResource);
				
			}
			
		}
		
		return userModel;
		
	}
}
